/*
    SlideScript - minimalistic top-down scripting language.
    (C) Copyright 2014-2022 Chris Dorman - some rights reserved (GPLv2)

    View README file supplied with this software for more details
*/


#include "inc/deps.h"
#include "inc/vars.h"
#include "inc/pipe.h"
#include "inc/util.h"

void set_cmd(int index, char *cmd)
{
    strcpy(spipe[index].command, cmd);
}

int get_cmd_count()
{
    int yy;
    for(yy = 0; yy < MAX_PIPE_CMDS; yy++)
    {
        if(strlen(spipe[yy].command) > 0)
        {
            continue;
        }
        else
        {
            return yy;
        }
    }
    return 0;
}

void ss_piping(char *string)
{
    char *pipe_tok;

    char *stringmem;

    for(int vv = 0; vv < MAX_PIPE_CMDS; vv++) bzero(spipe[vv].command,MAX_STRING_LEN);

    stringmem = (char *) malloc(strlen(string)+1);

    *stringmem = '\0';

    if(stringmem == NULL) syn_error("ss:error:memory mapping error...");

    sprintf(stringmem, "%s", string);

    pipe_tok = strtok(stringmem, "|");
    if(pipe_tok == NULL)
    {
        set_cmd(0, string);
    }
    else
    {
        set_cmd(0, pipe_tok);
    }

    while((pipe_tok = strtok(NULL, "|")) != NULL)
    {
        int cmdc = get_cmd_count();
        set_cmd(cmdc, pipe_tok);
    }

    free(stringmem);
}
